package com.gopay.common.trace.def;


import com.gopay.common.trace.enums.TraceType;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ddh02
 * @Span
 * @2017/3/30 9:59  www.gopay.com.cn Inc.All rights reserved.
 */


public class Span implements Serializable {

    /**trace id  跟踪id**/
    private String tid;

    /**自身id**/
    private String id;


    /**订单id**/
    private String oid;

    /**父id**/
    private String pid;

    /**名称**/
    private String name;

   // private String serviceId; // 暂时没用
   // private List<Annotation> annotations; //为了方便计算 将单个an 设置成成员变量

    /**附加信息**/
    private List<BinaryAnnotation> ba;

    /**是否采样 目前 没啥用**/
    private boolean isSample;

    private List<Span> childs = new ArrayList<>(); //父子关系


    /**trace type 跟踪类型**/
    private String ty = TraceType.RPC.toString();


    /**开始时间**/
    private long st;  //开始时间 弃用 annotation


    /**结束时间**/
    private long et;    //结束时间 弃用 annotation

   // private long middleTime;  //中间时间

    /**ip 端口 信息 比如springmvc  调用会有调用方的 ip+端口**/
    private Endpoint ep;


    /**
     * 层级关系
     */
    private String le;

    public Span(){
      //  annotations = new ArrayList<Annotation>();
     //   binaryAnnotations = new ArrayList<BinaryAnnotation>();
    }

    public void addAnnotation(Annotation a){
        //annotations.add(a);
    }

    public void addBinaryAnnotation(BinaryAnnotation a){
        if (ba == null) {
            ba = new ArrayList<BinaryAnnotation>();
        }
        ba.add(a);
    }

    public Endpoint getEp() {
        return ep;
    }

    public void setEp(Endpoint ep) {
        this.ep = ep;
    }

    /*public long getMiddleTime() {
        return middleTime;
    }

    public void setMiddleTime(long middleTime) {
        this.middleTime = middleTime;
    }*/


    public String getTid() {
        return tid;
    }

    public void setTid(String tid) {
        this.tid = tid;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<BinaryAnnotation> getBa() {
        return ba;
    }

    public void setBa(List<BinaryAnnotation> ba) {
        this.ba = ba;
    }

    public String getTy() {
        return ty;
    }

    public void setTy(String ty) {
        this.ty = ty;
    }

    public long getSt() {
        return st;
    }

    public void setSt(long st) {
        this.st = st;
    }

    public long getEt() {
        return et;
    }

    public void setEt(long et) {
        this.et = et;
    }

    public boolean isSample() {
        return isSample;
    }

    public void setSample(boolean sample) {
        isSample = sample;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLe() {
        return le;
    }

    public void setLe(String le) {
        this.le = le;
    }

    public String getOid() {
        return oid;
    }

    public void setOid(String oid) {
        this.oid = oid;
    }

    public List<Span> getChilds() {
        return childs;
    }

    public void setChilds(List<Span> childs) {
        this.childs = childs;
    }

    public void addChild(Span child) {
        child.setPid(this.getId());
        child.setTid(this.getTid());
        this.childs.add(child);
    }

    @Override
    public String toString() {
        return "Span{" +
                "tid='" + tid + '\'' +
                ", id='" + id + '\'' +
                ", oid='" + oid + '\'' +
                ", pid='" + pid + '\'' +
                ", name='" + name + '\'' +
                ", ba=" + ba +
                ", ty='" + ty + '\'' +
                ", st=" + st +
                ", et=" + et +
                ", le=" + le +
                ", ep=" + ep +
                '}';
    }

    public static void main(String[] args) {
        Span s1 = new Span();
        s1.setId("1");

        Span s1_1 = new Span();
        s1_1.setId("1_1");

        Span s1_2 = new Span();
        s1_2.setId("1_2");
        s1.addChild(s1_1);
        s1.addChild(s1_2);


        Span s1_1_1 = new Span();
        s1_1_1.setId("1_1_1");
        s1_1.addChild(s1_1_1);

       // s1.showTree(s1, 0);



    }

}
